package com.ydzt.fanka.server.utils;

import org.xvolks.jnative.JNative;
import org.xvolks.jnative.Type;
import org.xvolks.jnative.exceptions.NativeException;
import org.xvolks.jnative.pointers.Pointer;
import org.xvolks.jnative.pointers.memory.MemoryBlockFactory;

import com.ydzt.fanka.server.init.InitData;

/*
 * 调用与pos通信的动态库dll
 */
public class CommunicationPosNative {
	/*
	 * 功能:串口初始化
	 * 返回 成功返回1
	 *    失败返回0
	 * @param iPort串口号 1-256分别表示COM1-256
	 * @param timeOut 等待接收数据的最大超时, 单位毫秒
	 */
	public static final int openCOM(int iPort,long timeOut){
		JNative n = null;
		int result = -1;
		try{
			System.load(ConfigFileUtil.GetValueByKey(InitData.communication_pos));
			n = new JNative("SZGXComm.dll","OpenCOM");
			n.setRetVal(Type.INT);
			
			n.setParameter(0, Type.INT,"" + iPort);
			n.setParameter(1, Type.LONG, "" + timeOut);
			n.invoke();//调用方法
			result = n.getRetValAsInt();
		}
		catch(NativeException e){
			
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		}
		finally{
			if(n != null)
				try {
					n.dispose();
				} catch (IllegalAccessException e) {
					e.printStackTrace();
				} catch (NativeException e) {
					e.printStackTrace();
				}
		}
		return result;
	}
	
	
	/*
	 * 功能:关闭串口
	 * 返回 成功返回1
	 *    失败返回0
	 * @param iPort串口号 1-256分别表示COM1-256
	 */
	public static final int closeCOM(int iPort){
		JNative n = null;
		int result = -1;
		try{
			System.load(ConfigFileUtil.GetValueByKey(InitData.communication_pos));
			n = new JNative("SZGXComm.dll","CloseCOM");
			n.setRetVal(Type.INT);
			
			n.setParameter(0, Type.INT,"" + iPort);
			n.invoke();//调用方法
			result = n.getRetValAsInt();
		}
		catch(NativeException e){
			
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		}
		finally{
			if(n != null)
				try {
					n.dispose();
				} catch (IllegalAccessException e) {
					e.printStackTrace();
				} catch (NativeException e) {
					e.printStackTrace();
				}
		}
		return result;
	}
	
	
	public static final byte[] packetParse(byte[] rev){
		JNative n = null;
		int result = -1;
		byte[] revResult = new byte[rev.length];
		try{
			//System.load(ConfigFileUtil.GetValueByKey(InitData.communication_pos));
			n = new JNative("C://Windows//System32//SZGXComm.dll","PacketParse");
			// 为数组创建空间  
		    Pointer aArrByteInput = new Pointer(MemoryBlockFactory.createMemoryBlock( rev.length));
		    Pointer pArrByteOutput = new Pointer(MemoryBlockFactory.createMemoryBlock( rev.length));
		    Pointer errByteOutput = new Pointer(MemoryBlockFactory.createMemoryBlock( rev.length));
		    for (int i = 0; i < rev.length; i++) {
		    	aArrByteInput.setByteAt(i, (byte)0x00);
		    }
			
			n.setParameter(0, aArrByteInput);
			n.setParameter(1, rev.length);
			n.setParameter(2, pArrByteOutput);
			n.setParameter(3, errByteOutput);
			
			n.setRetVal(Type.INT);
			n.invoke();//调用方法
			
			result = n.getRetValAsInt();
			if(result == 0){
				for (int i = 0; i < rev.length; i++) {
			    	//aArrByteInput.setByteAt(i, (byte)0x00);
					revResult[i] = pArrByteOutput.getAsByte(i);
			    }
			}
		}
		catch(NativeException e){
			
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		}
		finally{
			if(n != null)
				try {
					n.dispose();
				} catch (IllegalAccessException e) {
					e.printStackTrace();
				} catch (NativeException e) {
					e.printStackTrace();
				}
		}
		return revResult;
	}
	
	
	/*
	 * 功能:获取随机数
	 * 返回 随机数
	 * @param 需要获取的随机数个数
	 */
	/*
	public byte[] getRand(int len){
		JNative n = null;
		byte[] result = new byte[len];
		try{
			System.load(ConfigFileUtil.GetValueByKey(InitData.communication_pos));
			n = new JNative("SZGXComm.dll","GetRand");
			n.setRetVal(Type.INT);
			
			n.setParameter(0, Type.INT,"" + iPort);
			n.invoke();//调用方法
			result = n.getRetValAsInt();
		}
		catch(NativeException e){
			
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		}
		finally{
			if(n != null)
				try {
					n.dispose();
				} catch (IllegalAccessException e) {
					e.printStackTrace();
				} catch (NativeException e) {
					e.printStackTrace();
				}
		}
		return result;
	}
	*/
}
